package com.zhu.user.controller;

import com.zhu.common.annotation.Token;
import com.zhu.user.model.User;
import com.zhu.user.service.AuthService;
import com.zhu.user.util.SysUtil;
import com.zhu.util.EmptyUtil;
import com.zhu.util.RespUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UnknownAccountException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by DELL on 2016/1/5.
 */
@Controller
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private AuthService authService;

    @RequestMapping
    @Token(key="LOGIN_TOKEN", save = true, remove = true)
    public String login(HttpServletRequest request, HttpServletResponse response, Model model){
        if("POST".equalsIgnoreCase(request.getMethod())) {
            String failReasonClass = (String) request.getAttribute("shiroLoginFailure");
            if (EmptyUtil.notNUllAndEmpty(failReasonClass)) {
                if (failReasonClass.equals(UnknownAccountException.class.getName())) {
                    model.addAttribute("reason", "无效的用户名");
                } else {
                    model.addAttribute("reason", "用户名和密码不匹配");
                }

                model.addAttribute("username", request.getParameter("username"));
            }
        }

        return "auth/login";
    }

    @RequestMapping("/success")
    public String success(HttpServletRequest request, HttpServletResponse response){
            return "redirect:/sys";
    }

}
